package com.x.reader.service;

import com.x.framework.Base;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import com.x.reader.dao.BookDao;
import com.x.reader.dao.FavouriteDao;
import com.x.reader.model.Book;
import com.x.reader.model.Favourite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FavouriteService extends BaseService {

    @Autowired
    private FavouriteDao favouriteDao;

    public List<Favourite> selectFavouriteList(Favourite favourite) {
        try {
            return favouriteDao.selectFavouriteList(favourite);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public Favourite insertFavourite(Favourite favourite) {
        try {
            Favourite f = new Favourite();
            f.setUserId(favourite.getUserId());
            f.setFavouriteId(favourite.getFavouriteId());
            this.delete(f);
            return this.insert(favourite);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int deleteFavourite(Favourite favourite) {
        try {
            String userId = favourite.getUserId();
            String favouriteId = favourite.getFavouriteId();
            Integer categoryId = favourite.getCategoryId();
            int flag = 0;
            if (Base.isNotNull(userId) && Base.isNotNull(favouriteId) && Base.isNotNull(categoryId)) {
                flag = this.delete(favourite);
            }
            return flag;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

}
